Page({
  data: {
    images: [],
    index: -1,
    theme: 'light'
  },
  onLoad() {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('init', ({images, index}) => {
      this.setData({ images: [...images], index })
    })

    this.setData({
      theme: wx.getSystemInfoSync().theme || 'light'
    })

    if (wx.onThemeChange) {
      wx.onThemeChange(({theme}) => {
        this.setData({theme})
      })
    }
  },
  onDelete(e) {
    const {
      detail: { index },
    } = e
    this.data.images.splice(index, 1)
    this.setData({
      images: [...this.data.images],
      index: index && index - 1
    })
    this.getOpenerEventChannel().emit('del', index)
    if (this.data.images.length < 1) this.onClose()
  },
  onClose() {
    wx.navigateBack()
  },
  onChange(e) {
    this.setData({
      index: e.detail.index
    })
  },
  onShareAppMessage() {
    return {
      title: '发布瞬间',
      path: '/page/publish/index'
    }
  },
})
